﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Irrlicht.Net.Video
{
    public class Material : ReferenceCounted
    {               
        #region PInvoke
        [DllImport("IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern bool Material_GetFlag(IntPtr material, MaterialFlags flag);
        [DllImport("IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern void Material_SetFlag(IntPtr material, MaterialFlags flag, bool value);
        [DllImport("IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern IntPtr Material_GetTexture(IntPtr material, uint i);
        [DllImport("IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern bool Material_IsTransparent(IntPtr material);
        [DllImport("IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern void Material_SetTexture(IntPtr material, uint i, IntPtr tex);
        #endregion

        //! Maximum number of texture an SMaterial can have.
        const uint MaxTextures = 4;

        public Material(IntPtr pointer)
            : base(pointer)
        {
        }

        public bool IsTransparent
        {
            get
            {
                return Material_IsTransparent(pointer);
            }
        }

        public bool BackfaceCulling
        {
            get
            {
                return GetFlag(MaterialFlags.BackFaceCulling);
            }

            set
            {
                SetFlag(MaterialFlags.BackFaceCulling, value);
            }
        }

        public bool FogEnabled
        {
            get
            {
                return GetFlag(MaterialFlags.FogEnabled);
            }

            set
            {
                SetFlag(MaterialFlags.FogEnabled, value);
            }
        }

        public bool FrontFaceCulling
        {
            get
            {
                return GetFlag(MaterialFlags.FrontFaceCulling);
            }

            set
            {
                SetFlag(MaterialFlags.FrontFaceCulling, value);
            }
        }

        public bool GouraudShading
        {
            get
            {
                return GetFlag(MaterialFlags.GouraudShading);
            }

            set
            {
                SetFlag(MaterialFlags.GouraudShading, value);
            }
        }

        public bool Lighting
        {
            get
            {
                return GetFlag(MaterialFlags.Lighting);
            }

            set
            {
                SetFlag(MaterialFlags.Lighting, value);
            }
        }

        public bool NormalizeNormals
        {
            get
            {
                return GetFlag(MaterialFlags.NormalizeNormals);
            }

            set
            {
                SetFlag(MaterialFlags.NormalizeNormals, value);
            }
        }

        public bool PointCloud
        {
            get
            {
                return GetFlag(MaterialFlags.PointCloud);
            }

            set
            {
                SetFlag(MaterialFlags.PointCloud, value);
            }
        }

        public bool WireFrame
        {
            get
            {
                return GetFlag(MaterialFlags.WireFrame);
            }

            set
            {
                SetFlag(MaterialFlags.WireFrame, value);
            }
        }

        public bool ZWriteEnabled
        {
            get
            {
                return GetFlag(MaterialFlags.ZWriteEnabled);
            }

            set
            {
                SetFlag(MaterialFlags.ZWriteEnabled, value);
            }
        }

        public bool GetFlag(MaterialFlags flag)
        {
            return Material_GetFlag(pointer, flag);
        }

        public void SetFlag(MaterialFlags flag, bool value)
        {
            Material_SetFlag(pointer, flag, value);
        }

        public Texture GetTexture(uint id)
        {
            return new Texture(Material_GetTexture(pointer, id));
        }

        public void SetTexture(uint id, Texture texture)
        {
            Material_SetTexture(pointer, id, texture.pointer);
        }
    }
}
